package Login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-10-21
 * Time: 19:46
 */
//这里代表登录页面的主页
@WebServlet("/index")
public class LoginIndex extends HttpServlet {

    //重定向是Get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //如果session没有,就重新登陆(不重新创建session对象),如果有则进行显示当前信息
        HttpSession session = req.getSession(false);
        if(session==null){
            resp.sendRedirect("login");
            return;
        }
        String userName = (String) session.getAttribute("userName");
        Integer visitCount = (Integer) session.getAttribute("visitCount");
        session.setAttribute("visitCount",visitCount+=1);
        resp.setContentType("text/html;charset=utf8");
        resp.getWriter().write("当前用户为 : " + userName + " 访问次数" + visitCount);
    }
}
